/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * PregledUposlenikaPanel.java
 *
 * Created on 13.01.2011., 00:12:19
 */

package hraplication;

import java.sql.ResultSet;
import java.sql.SQLException;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Fedja
 */
public class PregledUposlenikaPanel extends Paneli {
    private Konekcija kon;
    /** Creates new form PregledUposlenikaPanel */
    public PregledUposlenikaPanel(Konekcija konekcija) {
        initComponents();
        kon = konekcija;
        OsvjeziChBOX();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */

    public void OsvjeziChBOX(){
        DefaultTableModel m = (DefaultTableModel) tabela.getModel();
        m.getDataVector().removeAllElements();
        //Popunjavanje tebele
        ResultSet r = kon.Select("SELECT u.idUposlenici, u.ime, u.prezime, u.jmbg, o.naziv, p.naziv, u.username FROM uposlenici AS u, organizacione_jedinice AS o, pozicije AS p "+
                "WHERE u.Organizacione_jedinice_idOJ = o.idOJ AND u.Pozicije_idPozicije = p.idPozicije");
        if (r!=null){
            try{
                while (r.next()){
                    m.addRow(new Object[] {r.getInt(1), r.getString(7), r.getString(2), r.getString(3), r.getString(4), r.getString(5), r.getString(6)});
                }
            }
            catch (SQLException ex)
            {
              System.err.println(ex.getMessage());
            }
        }
        tabela.repaint();
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        tabela = new javax.swing.JTable();

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(hraplication.HRaplicationApp.class).getContext().getResourceMap(PregledUposlenikaPanel.class);
        setBorder(javax.swing.BorderFactory.createTitledBorder(resourceMap.getString("Form.border.title"))); // NOI18N
        setName("Form"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        tabela.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "ID", "Username", "Ime", "Prezime", "JMBG", "Org. jedinica", "Pozicija"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Integer.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tabela.setName("tabela"); // NOI18N
        tabela.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                tabelaKeyReleased(evt);
            }
        });
        jScrollPane1.setViewportView(tabela);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 514, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 262, Short.MAX_VALUE)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents
    @SuppressWarnings("static-access")
    private void tabelaKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tabelaKeyReleased
        if (evt.getKeyCode() == evt.VK_DELETE){
            int oznaceni[] = tabela.getSelectedRows();
            if (oznaceni.length==0){
                JOptionPane.showMessageDialog(this, "Oznacite red(ove) koje zelite izbrisati!", "Info",WIDTH,null);
                return;
            }     
            DefaultTableModel m = (DefaultTableModel) tabela.getModel();

            String kom = "DELETE FROM uposlenici WHERE idUposlenici="+m.getValueAt(oznaceni[0], 0);
            for (int i=1; i<oznaceni.length; i++){
                kom = kom + " OR idUposlenici="+m.getValueAt(oznaceni[i], 0);
            }
            String greska = kon.Delete(kom);
            if (greska != null)
                //DOSLO DO GRESKE
                JOptionPane.showMessageDialog(this, greska, "Greska",WIDTH,null);
            
            OsvjeziChBOX();
            tabela.clearSelection();

        }
        if (evt.getKeyCode() == evt.VK_ESCAPE){
            tabela.clearSelection();
        }
       
    }//GEN-LAST:event_tabelaKeyReleased


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tabela;
    // End of variables declaration//GEN-END:variables

}
